package com.extremelylargepackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.content.Context;

public class FileUtils 
{
	private FileUtils() { }
	
	public static final String CUSTOM_LEVEL_DIR = "level";
	public static final String TEST_LEVEL_NAME = "testLevel";
	
	public static File getCustomLevelsDir(Context c)
	{
		File approot = c.getFilesDir();
		File levelsDir = null;
		for(File f : approot.listFiles())
		{
			if(f.isDirectory() && f.getName().equals(CUSTOM_LEVEL_DIR))
			{
				levelsDir = f;
				break;
			}
		}
		if(levelsDir == null)
		{
			levelsDir = new File(approot.getAbsolutePath() + "/" + CUSTOM_LEVEL_DIR + "/");
			levelsDir.mkdir();
		}
		return levelsDir;
	}
	
	public static boolean customLevelExists(Context c, String lName)
	{
		File[] levels = getCustomLevelsDir(c).listFiles();
		for(int i = 0; i < levels.length; i++)
		{
			if(levels[i].getName().equals(lName))
				return true;
		}
		return false;
	}
	
	public static InputStream readCustomLevel(Context c, String levelName) throws FileNotFoundException
	{
		return new FileInputStream(new File(getCustomLevelsDir(c), levelName));
	}
	
	public static InputStream readTestLevel(Context c) throws FileNotFoundException
	{
		return c.openFileInput(TEST_LEVEL_NAME);
	}
	
}
